package com.controller
{
	import com.events.UserMasterEvent;
	import com.model.valueObjects.VOUserImage;
	import com.model.valueObjects.VOUserMaster;
	import com.service.UserMasterDetails;
	
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.Base64Decoder;

	public class UserMasterController extends AbstractController
	{
		import com.model.Model;
		[Inject]
		public var userMasterDetails:UserMasterDetails;
		
		public function UserMasterController()
		{
			super();
		}
		
		[EventHandler(event="UserMasterEvent.GET_IMAGE",properties="imgSerialnumber")]
		public function getImage(imgSerialnumber:Number):void{
			model.userImage = model.loadingImageByteArr;
			sh.executeServiceCall(userMasterDetails.getUserImage(imgSerialnumber),getImageResult, handleFault );
		}		
		private function getImageResult(event:ResultEvent):void
		{
			var imageVO:VOUserImage=event.result as VOUserImage;
			var byteArr:ByteArray;
			var base64Dec:Base64Decoder = new Base64Decoder();
			var str:String=String(imageVO.user_image);
			base64Dec.decode(str);
			byteArr = base64Dec.toByteArray();
			model.userImage =byteArr;
		}
		
		[EventHandler(event="UserMasterEvent.GET_ALL")]
		public function getAll():void
		{
			model.currentAdminTableIndex=0;	
			sh.executeServiceCall(userMasterDetails.getAllUsers(), getAllUserResult, handleFault );
		}		
		private function getAllUserResult(event:ResultEvent):void
		{
			model.userMasterDetails=event.result as ArrayCollection;;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
		}
		
		[EventHandler(event="UserMasterEvent.ADD", properties="userMaster,imageData")]
		public function addUser(userMaster:VOUserMaster,imageData:String):void
		{
			sh.executeServiceCall(userMasterDetails.addNewUser(userMaster,imageData), addUserResult, handleFault );	
		}
		private function addUserResult(event:ResultEvent):void
		{
			getAll();
			if(model.saveCloseBtn)model.currentForm=1;

			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("New User Added" );
			
		}
		
		[EventHandler(event="UserMasterEvent.UPDATE", properties="userMaster,imageData")]
		public function updateUser(userMaster:VOUserMaster,imageData:String):void
		{
			sh.executeServiceCall(userMasterDetails.editUserDetails(userMaster,imageData),updateUserResult,handleFault );
		}
		
		private function updateUserResult(event:ResultEvent):void
		{
			getAll();
			model.currentForm=1;
			CursorManager.removeBusyCursor();
			model.busyCurrsorOn=true;
			Alert.show("Details Updated ");
		}
		
		[EventHandler(event="UserMasterEvent.DELETE", properties="serialNo")]
		public function deleteUser(serialNo:int):void
		{
			sh.executeServiceCall(userMasterDetails.deleteUserDetails(serialNo), deleteUserResult, handleFault );	
		}
		
		private function deleteUserResult(event:ResultEvent):void
		{
			getAll();
			model.currentForm=1;
			CursorManager.removeBusyCursor();
			Alert.show("Record Deleted " );
		}
		
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}